#include <QApplication>
#include <QWidget>
#include <QMessageBox>
#include <QTranslator>     // Include for QTranslator
#include <QLibraryInfo>  // Include for QLibraryInfo (to find translations path)
#include <QLocale>       // Include for QLocale
#include "loginwindow.h"
#include "mainwindow.h"


int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    // --- Load Qt standard translations ---
    QTranslator qtTranslator;
    // Explicitly try to load Chinese translation
    QString translationsPath = QLibraryInfo::path(QLibraryInfo::TranslationsPath);
    bool loaded = qtTranslator.load("qt_zh_CN", translationsPath);

    // Fallback: Try loading from executable's directory/translations
    if (!loaded) {
        loaded = qtTranslator.load("qt_zh_CN", QCoreApplication::applicationDirPath() + "/translations");
    }

    if (loaded) {
        app.installTranslator(&qtTranslator);
    } else {
        QMessageBox::warning(nullptr, "翻译加载失败",
                             QString("未能加载 Qt 中文翻译文件 (qt_zh_CN.qm)。\n请确保该文件存在于以下路径之一：\n1. %1\n2. [程序目录]/translations\n\n日期和标准对话框可能显示英文或乱码。")
                             .arg(translationsPath));
    }
    // --- End loading translations ---

    LoginWindow login;
    login.show();

    // Update lambda to accept userId
    QObject::connect(&login, &LoginWindow::loginSuccess, [&](const QString& username, int userId, int userType){
        // 登录成功后隐藏登录窗口，显示主界面
        // Pass userId to MainWindow constructor
        MainWindow* mainWin = new MainWindow(username, userId, userType);
        mainWin->show();
        login.close();
    });

    return app.exec();
}